//
//  HQBigImageViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/6/5.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQBigImageViewController.h"

@interface HQBigImageViewController ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    UIImageView *_imageView;
    __weak IBOutlet UIButton *_closeItem;
}
@end

@implementation HQBigImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupUI];
    [_closeItem bringSubviewToFront:self.view];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49)];
    _scrollView.maximumZoomScale = 2.0;
    _scrollView.minimumZoomScale = 1.0;
    
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    //显示照片的imageView
    _imageView = [[UIImageView alloc]init];
    _imageView.userInteractionEnabled = YES;
    _imageView.center = self.view.center;
    //长按保存手势
//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
//    [_imageView addGestureRecognizer:longPress];
    [_scrollView addSubview:_imageView];
    //双击缩放手势
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    doubleTap.numberOfTapsRequired = 2;
    [_imageView addGestureRecognizer:doubleTap];
    
    [self loadImage];
    
}
- (IBAction)closeItem:(id)sender {
    NSLog(@"点击了按钮");
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadImage {
    if(self.imageData){
        _imageView.image = self.imageData;
        //设置imageView的位置
        [self setImagePosition:_imageView.image ];
    }else{
        [_imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.imageURL]] placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
            //如果图片下载失败 则直接返回
            if (image == nil) {
                return ;
            }
            _imageView.image = image;
            //设置imageView的位置
            [self setImagePosition:image];
        } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
            
        }];
    }
}

- (void)setImagePosition:(UIImage *)image {
    CGSize size = [self imageSizeWithScreen:image];
    NSLog(@"%f %f",size.width,size.height);
    _imageView.frame = CGRectMake(0, 0, size.width, size.height);
    _scrollView.contentSize = size;
    if (size.height < _scrollView.bounds.size.height) {
        CGFloat offsetY = (_scrollView.bounds.size.height - size.height) * 0.5;
        _scrollView.contentInset = UIEdgeInsetsMake(offsetY, 0, offsetY, 0);
    }
}

-(CGSize)imageSizeWithScreen:(UIImage *)image{
    return CGSizeMake(SCREEN_WIDTH,(SCREEN_WIDTH/(image.size.width/ image.size.height)));
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return _imageView;
}
/** 计算点击点所在区域frame */
- (CGRect)getRectWithScale:(CGFloat)scale andCenter:(CGPoint)center{
    CGRect newRect = CGRectZero;
    newRect.size.width =  _scrollView.frame.size.width/scale;
    newRect.size.height = _scrollView.frame.size.height/scale;
    newRect.origin.x = center.x - newRect.size.width * 0.5;
    newRect.origin.y = center.y - newRect.size.height * 0.5;
    return newRect;
}

- (void)doubleTap:(UITapGestureRecognizer *)recognizer {
    if (_scrollView.zoomScale > 1.0) {
        [_scrollView setZoomScale:1.0 animated:YES];
    } else {
        CGPoint touchPoint = [recognizer locationInView:_imageView];
        CGFloat scale = _scrollView.maximumZoomScale;
        CGRect newRect = [self getRectWithScale:scale andCenter:touchPoint];
        [_scrollView zoomToRect:newRect animated:YES];
    }
}

@end
